package eyrolles;

import exceptions.DataSourceException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import modele.InterfaceEmployeModel;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

public class EyrollesPlugin implements PlugIn {

    public static final String PROPERTIES = "PROPERTIES";
    private String filePath = null;
    private InterfaceEmployeModel employeModele = null;
    private File file = null;
    private Properties props = new Properties();
    private String target = "success";
    private ActionMessages errors = new ActionMessages();

    @Override
    public void init(ActionServlet servlet, ModuleConfig applicationConfig)
            throws javax.servlet.ServletException {
        System.out.println("---->Le plug-in demarre<----");
        ServletContext context = servlet.getServletContext();

        try {
            // On récupère le fichier de properties
            String fileName = getFilePath() + "/db.properties";
            file = new File(fileName);
            FileInputStream is = new FileInputStream(file);
            props.load(is);
        } catch (FileNotFoundException e) {
            target = "error";
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                       new ActionMessage("errors.properties.error"));
        } catch (IOException e) {
            target = "error";
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                       new ActionMessage("errors.properties.error"));
        }

        // Signaler les erreurs éventuelles
        if (!errors.isEmpty())
            throw new RuntimeException("ERROR");

        try {
            // On instancie le modèle
            employeModele = (InterfaceEmployeModel) Factory.getObject(props.getProperty("employes.modele"));
            employeModele.setDataSource(getDataSource());
            context.setAttribute("ModeleEmploye", employeModele);
        } catch (ClassNotFoundException e) {
            target = "error";
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                       new ActionMessage("errors.database.error"));
        } catch (InstantiationException e) {
            target = "error";
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                       new ActionMessage("errors.database.error"));
        } catch (IllegalAccessException e) {
            target = "error";
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                       new ActionMessage("errors.database.error"));
        } catch (DataSourceException e) {
            target = "error";
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                       new ActionMessage("errors.database.error"));
        }
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String f) {
        filePath = f;
    }

    @Override
    public void destroy() {
        System.out.println("---->Le plug-in s'arrete<----");
    }

    public DataSource getDataSource() throws DataSourceException {

        try {
            Context initCtx = new InitialContext();
            DataSource ds = (DataSource) initCtx.lookup(props.getProperty("POOLresource"));
            return ds;
        } catch (NamingException ex) {
            throw new DataSourceException();
        }
    }
}